package com.example.zzm.yuejian03.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Activity.ClubActivity;
import com.example.zzm.yuejian03.Bean.ClubBean;
import com.example.zzm.yuejian03.R;
import com.example.zzm.yuejian03.Server;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ClubAdapter extends RecyclerView.Adapter {
    private List<ClubBean> list;//向rv中填充的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局

    //自定义 构造方法

    public ClubAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    //自定义 ViewHolder子类，容纳item视图
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
        }
    }

    //自定义 设置数据list
    public void setList(List<ClubBean> list) {
        this.list = list;
        notifyDataSetChanged();//通知RV刷新数据
    }

    @NonNull
    @Override  //为每一个item生成一个view
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(
                R.layout.club, parent, false);

        return new ViewHolder(v);
    }

    @Override  //为每个item填充数据，设置点击事件
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final ClubBean bean = list.get(position);
        if (bean == null)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        Picasso.with(context)//新版的Picasso方法改为get()
                .load(Server.IMAGEURL + bean.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.textView.setText(bean.getClub_name());

        //item条目点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //取出当前item的id
                int cl_id = bean.getid();
                Toast.makeText(context, "" + cl_id, Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(context, ClubActivity.class);
                intent.putExtra("club_id",bean.getid());
                context.startActivity(intent);

            }
        });
    }

    @Override  //确定item个数
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }

}
